package cn.exrick.xboot.factory;

import cn.exrick.xboot.base.XbootBaseEntity;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: njp
 * @Date: 2020/09/07/17:47
 * @Description:
 */
public class MyBeanFactory {

    private static final MyBeanFactory m=new MyBeanFactory();

    private MyBeanFactory(){};
    public static MyBeanFactory getMyBeanFactory(){
        return m;
    }
    /**
     * bean工厂
     * 为对象赋值时间
     * @param c
     * @param <T>
     * @return
     */
    public  <T extends XbootBaseEntity> T createBean(Class<T> c){
        Date date = new Date();
        XbootBaseEntity x=null;
        try {
            x=(T)Class.forName(c.getName()).newInstance();
            x.setCreateTime(date);
            x.setUpdateTime(date);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return (T)x;
    }

    /**
     * 统一设置修改时间
     * @param c
     * @param <T>
     * @return
     */
    public  <T extends XbootBaseEntity> T editBean(Class<T> c){
        Date date = new Date();
        XbootBaseEntity x=null;
        try {
            x=(T)Class.forName(c.getName()).newInstance();
            x.setUpdateTime(date);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return (T)x;
    }

}
